package com.amat0urheu.service.cloudService.impl;

import com.amat0urheu.dao.CloudMapper;
import com.amat0urheu.dao.FlavorMapper;
import com.amat0urheu.pojo.CloudPojo.Flavor;
import com.amat0urheu.pojo.CloudPojo.OpenStack;
import com.amat0urheu.pojo.ReturnRes;
import com.amat0urheu.service.cloudService.FlavorService;
import com.amat0urheu.utils.CloudUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FlavorServiceImpl implements FlavorService {
    @Autowired
    CloudMapper cloudMapper;
    @Autowired
    FlavorMapper flavorMapper;

    @Override
    public ReturnRes getAllFlavors(Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Flavor> flavors = flavorMapper.getFlavors();
        if (flavors != null) {
            return new ReturnRes(true, new PageInfo<>(flavors), "查询成功");
        }
        return new ReturnRes(false, "查询失败");
    }

    @Override
    public ReturnRes CreateFlavor( Flavor flavor) {
        List<OpenStack> allOpenstack = cloudMapper.getAllOpenstack();
        String id="";
        for (int i=0;i<allOpenstack.size();i++){
                CloudUtils.createflavor(flavor,allOpenstack.get(i));
        }

        flavorMapper.insertFlavors(flavor);
        return new ReturnRes(true, "创建类型成功");
    }

    @Override
    public ReturnRes removeFlavor(String flavorName) {
        List<OpenStack> allOpenstack = cloudMapper.getAllOpenstack();
        for (OpenStack o:allOpenstack) {
            CloudUtils.removeflavor(o,flavorName);
        }
        int count =flavorMapper.deleteFlavorId(flavorName);
        if(count>0){
            return new ReturnRes(true, "删除成功");
        }
         return new ReturnRes(false, "删除失败");
    }

    @Override
    public ReturnRes refreshFlavor() {
        List<Flavor> flavors = CloudUtils.getflavors(cloudMapper.getOpenstackByopenstackIP("192.168.201.17"));
        int i=0;
          flavorMapper.dropFlavors();
        for (Flavor flavor : flavors) {
            flavorMapper.insertFlavors(flavor);
                i++;
        }
        if(i!=0){
            System.out.println("刷新成功");
        }

        return new ReturnRes(true, "刷新成功");
    }

    @Override
    public ReturnRes getAllFlavorsName() {

        List<Flavor> flavors = flavorMapper.getAllFlavorsName();
        if (flavors != null) {
            return new ReturnRes(true, new PageInfo<>(flavors), "查询成功");
        }
        return new ReturnRes(false, "查询失败");
    }
}